from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
from root import application as app

class htmlbuilder:
        
    back_btn_src = "../static/images/back_button.png" 
    def print_header(self):
        header = """
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

         "http://www.w3.org/TR/html4/loose.dtd"> 
    
        <html>

        <link rel="stylesheet" type="text/css" href="stylesheets/main.css" />
        <head>

        </head>"""
        return header
    
    def print_footer(self):
        
        footer = """</body>

        </html>"""
        return footer
    

    def get_html_code(self, root_dir, src):
        
        path = os.path.join(root_dir, src)
        f = open(path, 'r')
        html = f.read()
        f.close()
        return html

    def print_body(self,day,events):
       
        root_dir = os.path.dirname(app.__file__)
        back_button = self.get_html_code(root_dir, 'html/back_button.html')
        home_button = self.get_html_code(root_dir, 'html/home_button.html')        
        refresh_button = self.get_html_code(root_dir, 'html/refresh_button.html')
           
        body = ""
        body_title = """<body> <p class = "side_title"> <font size = "30">"""+ day +"""</font> """
        
        title_close = """</p>"""
        
        body+=body_title + back_button + refresh_button + home_button + title_close
        body+="""<div class = day_items align=center>"""
        
        body+= self.print_multiple_containers(events)
        
        body+="""</div>"""
        body += "</body>"
        
       # body = "Root path: " + path
         
        return body
    
    def print_multiple_containers(self,containers):
        containers_result=""
        for event in containers:
            containers_result += self.print_container(event)
        
        return containers_result
    
        
        
    def print_icons(self, icons):
        icon_container ="""<div class = "entry_details">"""
        
        for icon in icons:
            icon_list ="""<img class = "zoom" src="images/"""
            icon += ".png" +'"'
            icon +=""" alt="">"""
            icon_list+=icon
            icon_container+=icon_list
            
        icon_container+="""</div>"""
        return icon_container
    
    
    '''
    Instead of day as an argument, why not use  key-value pair
    and make this function generic for days, genres etc etc?
    '''
    def generate(self,events, day):
        
        finalstring=""
        finalstring += self.print_header()
        finalstring += self.print_body(day,events)
        finalstring += self.print_footer()
        return finalstring;
    
    def create_club_icon(self, url, icon):
        if url =="":
            return ""
        if icon =="":
            return ""
        
        result=""
        result += "<a href=" +'"' + url + '"' + " target="+'"'+"_blank" +'"' +'>'
        
        result += """<img class = "profilezoom" src=""" + "" +'"'+ icon +'"' +" alt"+'>'
        result += "</a>"
            
        return result
    
    def print_container(self, event):
        container=""
        container+="""<div class = "container">"""
        
        #container += self.print_title(event['name'], event['location']) 
        container += self.create_club_icon(event['link'], event['profile_link'])
        container += self.print_age_limit(event['age'])
        container += self.print_vip(event['VIP'])
        container += self.print_promoted(event['promoted'])
        container += self.print_start_time(event['start_time'])
        container += self.print_location(event['location'])
        container += self.print_more_information();
       
        container+="""</div>"""
        return container
    
    def print_title(self, name, location):
            result= "<h2>" + name + "<h2/> <b/> <h3>" + location + "<h3/>"  
            return result
   
    def print_more_information(self):
        result="<a href=" +'"' + "\EventDetails" + '"' + '>'
        result += """<img class = "info" src=""" +'"'+"images/MoreInformation.png" +'"' +" alt" +'>'
        return result
    
    def print_club_icon(self,url):
        
        if url =="":
            return ""
        
        result ="""<img class = "zoom" src="images/"""
        result += url +".png" +'"'
        result +=""" alt="">"""
        return result;
    
    def print_event_name(self,name):
        
        if name =="":
            return ""
        
        result ="""<img class = "zoom" src="images/"""
        result += name +".png" +'"'
        result +=""" alt="">"""
        return result;
        
        return result;
    
    def print_age_limit(self, age):
        
        if age =="":
            return ""
        
        result ="""<img class = "zoom" src="images/"""
        result += age +".png" +'"'
        result +=""" alt="">"""
        return result;
    
    def print_vip(self,vip):
        if vip =="":
            vip = "VIP_GREY"
        
        result ="""<img class = "zoom" src="images/"""
        result += vip +".png" +'"'
        result +=""" alt="">"""
        return result;
        
    
    def print_promoted(self, promoted):
        if promoted =="":
            promoted = "Promoted_GREY"
        
        result ="""<img class = "zoom" src="images/"""
        result += promoted +".png" +'"'
        result +=""" alt="">"""
        return result;
    
    def print_start_time(self, start_time):
        
        if start_time =="":
            return ""
       
        start_time = str(int(start_time[0:2]) -12 ) 
        result ="""<img class = "zoom" src="images/"""
        result += start_time +".png" +'"'
        result +=""" alt="">"""
        return result;
    
    def print_location(self,url):
        if url =="":
            return ""
        
        result =""
        url = self.put_into_gmaps_url(url)
        result += "<a href=" +'"' +url+'"' + " target=" +'"' +"_blank" +'"' +'>'
        
        result += """<img class = "zoom" src=""" +'"' +"images/Location.png" +'"' +'>'
        result += "</a>"
            
        return result
            
    def put_into_gmaps_url(self,addr):
        encoded = ""
        
        for c in addr:
            
            if c == ' ':
                c = '+'
                
            encoded += c
        url = "https://maps.google.ie/maps?q=" + encoded
        return url
        

